package leetcode.to300;

import java.util.HashMap;

/**
 * @ClassName T0001
 * @Description 两数之和
 * @Author Fanxc
 * @DATE 2021/1/22 12:34
 * @Version 1.0
 */
public class T0001 {
    //暴力法
    class Solution{
        public int[] twoSum(int[] nums,int target){
            int n =nums.length;
            for (int i = 0; i < n; i++) {
                for (int j = i+1; j < n; j++) {
                    if (nums[i]+nums[j]==target){
                        return new int[]{i,j};
                    }
                }
            }
            return new int[0];
        }
    }
    //hash法
    class Solution2{
        public int[] twoSum(int[] nums,int target){
            HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
            int n= nums.length;
            for (int i = 0; i < n; i++) {
                if (hashMap.containsValue(target-nums[i])){
                    return new int[]{i,hashMap.get(target-nums[i])};
                }
                hashMap.put(i, nums[i]);
            }
            return new int[0];
        }
    }
}
